package com.qinyh.view;

import java.util.ArrayList;
import java.util.List;

import com.qinyh.activity.R;
import com.qinyh.controller.Controller;
import com.qinyh.entity.Book;
import com.qinyh.entity.BookCatalog;
import com.qinyh.entity.Shelf;
import com.qinyh.util.DebugUtil;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.Display;
import android.view.MotionEvent;
import android.widget.ListView;
import android.widget.Toast;

public class ShelfView extends ListView {

	private Activity activity;
	// 翻页控制类
	private Controller controller;
	// 书架
	private Shelf shelf = null;

	private Context ctt;
	private AttributeSet attrs;
	
	/**
	 * 构造方法初始化书架
	 * 
	 * @param context
	 * @param attrs
	 */
	public ShelfView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.ctt = context;
		this.attrs = attrs;
		controller = new Controller(context);
		try {
			this.shelf = controller.getShelf();
		} catch (Exception e) {
			DebugUtil.debug("debug", e);
		}
	}

	/**
	 * 设置书架视图
	 * 
	 * @param activity
	 */
	public void setActivity(Activity activity) {
		try {
			this.activity = activity;
			List<BookCatalog> lstring = new ArrayList<BookCatalog>();
			int id = R.drawable.shelf_panel;
			if (shelf != null) {
				for (BookCatalog bc : shelf.getCatalog()) {
					lstring.add(bc);
					id = bc.getId();
				}
				int add = 6 - lstring.size();
				BookCatalog bc1 = new BookCatalog();
				bc1.setId(id);
				bc1.setBooks(null);
				for (int i = 0; i < add; i++) {
					lstring.add(bc1);
				}
				MyAdapter aa = new MyAdapter();
				aa.setConext(ctt);
				aa.setCatalog(lstring);
				aa.setActivity(activity);
				this.setAdapter(aa);
				this.setSaveEnabled(true);
			}
		} catch (Exception e) {
			DebugUtil.debug("debug", e);
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return super.onTouchEvent(event);
	}

}
